<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/3/17
 * Time: 下午4:18
 */
class OdsProductController extends BaseController{
    /*
     * 查询产品包详情
     * */
    public function package_show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'product_id' => 'required',
                'order_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $product_id = Input::get('product_id');
            $order_id = Input::get('order_id');

            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                //获取用户车型信息
                $order = Orders::where('order_id',$order_id)->first();
                //获取配件和服务信息
                $product_items = ProductPackItem::get_package_item($product_id,$order->car_model_id);
            }else{
                Log::error("error", array("system" => "ods" ,"event" => "package_show", 'input' =>Input::all()));
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'count' => count($product_items),
                    'product_items' => $product_items
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "package_show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 查询上门车产品包
     * */
    public function get_product_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');

            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                //获取产品包
                $product_packages = ProductPackage::where('place',1)
                    ->where('status',1)
                    ->select(array('id','name','desc'))
                    ->get()
                    ->toArray();
            }else{
                Log::error("error", array("system" => "ods" ,"event" => "get_product_list", 'input' =>Input::all()));
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'count' => count($product_packages),
                    'product_packages' => $product_packages
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "get_product_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}